package pub.cleangao.codegen.enquiry.oracle;

import pub.cleangao.codegen.GenConfigHolder;
import pub.cleangao.codegen.enquiry.ColumnEnquiry;
import pub.cleangao.codegen.enquiry.TableEnquiry;
import pub.cleangao.codegen.enquiry.TableInfo;
import pub.cleangao.codegen.util.ClassField;
import java.util.Map;



public class OracleTableEnquiry extends TableEnquiry {

    public OracleTableEnquiry(GenConfigHolder config, ColumnEnquiry columnEnquiry) {
        super(config, columnEnquiry);
    }

    @Override
    public String getShowTablesSql() {
        StringBuilder sb = new StringBuilder("");
        sb.append(" SELECT a.TABLE_NAME as NAME,b.COMMENTS" +
                "  FROM ALL_TABLES a,USER_TAB_COMMENTS b" +
                "  WHERE a.TABLE_NAME=b.TABLE_NAME");
        sb.append(" AND 1=1 ");
        if(this.schemaTables!= null && this.schemaTables.size() > 0) {
            StringBuilder tables = new StringBuilder();
            for (String table : this.schemaTables) {
                tables.append(",'").append(table).append("'");
            }
            sb.append(" AND a.TABLE_NAME IN (").append(tables.substring(1)).append(")");
        }
        return sb.toString();
    }

    @Override
    public TableInfo buildTableInfo(Map<String, Object> tableMap) {
        TableInfo tableInfo = new TableInfo();
        tableInfo.setTableName(ClassField.convertString(tableMap.get("NAME")));
        tableInfo.setTableNameHyphen(tableInfo.getTableName().replace("_","-"));
        tableInfo.setComment(ClassField.convertString(tableMap.get("COMMENTS")));
        return tableInfo;
    }
}
